package com.estar.worktime.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.antlr.grammar.v3.ANTLRv3Parser.exceptionGroup_return;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.estar.app.business.mobileLogin.entity.AppUserEntity;
import com.estar.workparent.controller.TmblWorktimeParentController;
import com.estar.workparent.entity.TmblWorktimeParentEntity;
import com.estar.workparent.service.TmblWorktimeParentServiceI;
import com.estar.worktime.entity.TmblWorktimeEntity;

@Component("workByDay")
@Scope("prototype")
public class WorkByDay {
	
	private static final Logger logger = Logger.getLogger(WorkByDay.class);
	private DateFormat dd=new SimpleDateFormat("yyyy-MM-dd");
	private DateFormat dt=new SimpleDateFormat("HH:mm:ss");
	private DateFormat dmonth=new SimpleDateFormat("MM");
	private DateFormat d=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private static String onTime="08:00:00";
	private static String offTimeHighWay="22:00:00";
	@Autowired
	private TmblWorktimeParentServiceI tmblWorktimeParentService;
	@Autowired
	private SystemService systemService;
	
	public void updateWorkMess(){
		try{
			Date dyes= yesterday(new Date()) ;//获取昨天的日期
			CriteriaQuery cq=new CriteriaQuery(TmblWorktimeEntity.class);
			cq.eq("workdate", dd.parse(dd.format(dyes)));
			cq.add();
			List<TmblWorktimeEntity> wl=systemService.getListByCriteriaQuery(cq, false);
			if(wl!=null && wl.size()>0){
				for(TmblWorktimeEntity tw:wl){
					if(StringUtil.isNotEmpty(tw.getEmpcdoe())){
						//打卡状态
						if(tw.getOffdutycard()==null && tw.getOndutycard()==null){//都不打卡
							tw.setNocar("t");
							tw.setWork("f");
							tw.setNowork("t");
						}else if(tw.getOffdutycard()!=null && tw.getOndutycard()==null){//上班缺卡
							tw.setWork("oc");
						}else if(tw.getOffdutycard()==null && tw.getOndutycard()!=null){//下班缺卡
							tw.setWork("fc");
						}else{//都打卡
							tw.setNocar("f");
						}
						
						if(StringUtil.isNotEmpty(tw.getLateflage()) && StringUtil.isNotEmpty(tw.getLeaveearlyflage())){
							if(tw.getLateflage().equals("f") && tw.getLeaveearlyflage().equals("f")){
								tw.setWork("t");
							}else if(tw.getLateflage().equals("t") && tw.getLeaveearlyflage().equals("t")){
								tw.setWork("el");
							}else if(tw.getLateflage().equals("t") && tw.getLeaveearlyflage().equals("f")){
								tw.setWork("l");
							}else if(tw.getLateflage().equals("f") && tw.getLeaveearlyflage().equals("t")){
								tw.setWork("e");
							}
						}
						
						//一天工作总时间
						Date onDuty=tw.getOndutytime();
						Date offDuty=tw.getOffdutytime();
						if(onDuty!=null && offDuty!=null){//都打卡
							tw.setWorktime(time(offDuty,onDuty));
						}else if(onDuty==null && offDuty!=null){//只有下班打卡
							onDuty=dt.parse(onTime);
							tw.setWorktime(time(offDuty,onDuty));
						}else if(onDuty!=null && offDuty==null){//只有上班打卡
							offDuty=getOffTime(tw, dyes);
							tw.setWorktime(time(offDuty,onDuty));
						}else{//都不打卡
							tw.setWorktime("00:00:00");
						}
						
						systemService.updateEntitie(tw);
					}
				}
			}
		}catch(Exception e){
			logger.error("后勤定时器系统错误：",e);
		}
	}
	
	private Date yesterday(Date date){
		Calendar c1=Calendar.getInstance();
		c1.setTime(date);
		c1.set(Calendar.DATE, c1.get(Calendar.DATE)-1);	
		return c1.getTime();
		
	}
	
	private String time(Date off,Date on){
		long l=0;
		 l=(off.getTime()-on.getTime());
		
		 long day=l/(24*60*60*1000);
		 long hour=(l/(60*60*1000)-day*24);
		 long min=((l/(60*1000))-day*24*60-hour*60);
		 long s=(l/1000-day*24*60*60-hour*60*60-min*60);
		 String Leftearlytime=hour+":"+min+":"+s;
		 return Leftearlytime;
	}
	private Date getOffTime(TmblWorktimeEntity tw,Date day) throws ParseException{
		List<AppUserEntity> a=systemService.findByProperty(AppUserEntity.class, "empCde", tw.getEmpcdoe());
		Date d1=new Date();
		String ri=dd.format(day);
		if(a!=null && a.size()>0){
			if(a.get(0).getArea().equals("2")){//城区和高速有不同的规定下班时间
				d1=d.parse(ri+" "+offTimeHighWay);
			}else if(a.get(0).getArea().equals("1")){
				d1=d.parse(ri+" "+chooseOffTime(dmonth.format(day)));
			}
		}else{//查无此用户，也给个时间
			ri=ri+" 20:00:00";
			d1=d.parse(ri);
		}
		return d1;
	}
	private String chooseOffTime(String m){
		String offTimeCity="19:00:00";
		if(m.equals("05") || m.equals("06") ||m.equals("07") ||m.equals("08") ||m.equals("09") ){
			offTimeCity="19:30:00";
		}
		return offTimeCity;
		
	}
}
